library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.1.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
Import dataset
data(nyc_airbnb)
nyc_airbnb =
nyc_airbnb |>
mutate(rating = review_scores_location / 2) |>
select(
neighbourhood_group, neighbourhood, rating, price, room_type, lat, long) |>
filter(
!is.na(rating),
neighbourhood_group == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500)
markers produces the same kind of plot as
ggplot::geom_point, lines produces the same
kind of plot as ggplot::geom_line. str_c join
text “Price: $” with value in variable price into single
character string. \n make a new line in string variable
nyc_airbnb |>
mutate(text_label = str_c("Price: $", price, "\nRating: ", rating)) |>
plot_ly(
x = ~lat, y = ~long, type = "scatter", mode = "markers",
color = ~price, text = ~text_label, alpha = 0.5)
nyc_airbnb |>
mutate(neighbourhood = fct_reorder(neighbourhood, price)) |>
plot_ly(y = ~price, color = ~neighbourhood, type = "box", colors = "viridis")
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(x = ~neighbourhood, y = ~n, color = ~neighbourhood, type = "bar", colors = "viridis")
scatter_ggplot =
nyc_airbnb |>
ggplot(aes(x = lat, y = long, color = price)) +
geom_point(alpha = 0.25) +
coord_cartesian()
ggplotly(scatter_ggplot)
box_ggplot =
nyc_airbnb |>
mutate(neighbourhood = fct_reorder(neighbourhood, price)) |>
ggplot(aes(x = neighbourhood, y = price, fill = neighbourhood)) +
geom_boxplot() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(box_ggplot)
# rmarkdown::render("dashboard_template.Rmd", output_format = "flexdashboard::flex_dashboard")